package com.api.shopapi.common;

import org.json.simple.JSONObject;

import com.xiaomi.xmpush.server.Constants;
import com.xiaomi.xmpush.server.Message;
import com.xiaomi.xmpush.server.Result;
import com.xiaomi.xmpush.server.Sender;

public class MiPushUtils {

    private final static int PASS_THROUGH_MESSAGE = 1;// 1��ʾ͸����Ϣ
    private final static int NOTIFICATION_MESSAGE = 0;// 0��ʾ֪ͨ����Ϣ
    private final static int PASS_THROUGH = NOTIFICATION_MESSAGE;// 1��ʾ͸����Ϣ��0��ʾ֪ͨ����Ϣ

    private final static int DEFAULT_ALL = -1;
    private final static int DEFAULT_SOUND = 1; // ʹ��Ĭ����ʾ����
    private final static int DEFAULT_VIBRATE = 2; // ʹ��Ĭ������ʾ
    private final static int DEFAULT_LIGHTS = 4; // ʹ��Ĭ��led�ƹ���ʾ
    private final static int NOTIFY_TYPE = DEFAULT_ALL;

    private String ANDROID_APP_SECRET = "uujlH6QuGE2R8/leeKAkSw==";
    private String IOS_APP_SECRET = "";
    private String ANDROID_PACKAGE_NAME = "com.ynnqo.yq";
    private static int DEVELOPMENT_MODE = 1; // 1=��ʽ����/0=����

    public final static int TYPE_ANDROID = 0;
    public final static int TYPE_IOS = 1;

    /**
     * ���캯��
     */
    public MiPushUtils(String androidAppSecret, String androidPackageName, String iosAppSecret, int developmentMode) {
        this.ANDROID_APP_SECRET = androidAppSecret;
        this.ANDROID_PACKAGE_NAME = androidPackageName;
        this.IOS_APP_SECRET = iosAppSecret;
        MiPushUtils.DEVELOPMENT_MODE = developmentMode;
    }

    /**
     * ����С������
     */
    public static void reStartPush(int deviceType) {
        // ���Ϊ���Ի���
        if (DEVELOPMENT_MODE == 0) {
            // ���Ի���ֻ�ṩ��IOS֧�֣���֧��Android
            Constants.useSandbox();
            if (deviceType == TYPE_ANDROID) {
                Constants.useOfficial();
            }
        } else {
            // ��ʽ����
            Constants.useOfficial();
        }
    }

    /**
     * ����android������Ϣ
     *
     * @param title
     * @param content
     * @param jsonObjectPayload
     * @param timeToSend
     * @return
     */
    private Message buildMessage2Android(String title, String content, JSONObject jsonObjectPayload, long timeToSend)
            throws Exception {
        Message message = new Message.Builder().title(title).description(content)
                .payload(jsonObjectPayload.toJSONString()).restrictedPackageName(ANDROID_PACKAGE_NAME)// ���ð���
                .passThrough(PASS_THROUGH) // ��Ϣʹ��͸����ʽ
                .notifyType(NOTIFY_TYPE) // ʹ��Ĭ����ʾ����ʾ
                .enableFlowControl(true) // ������Ϣ�Ƿ���Ҫ����ƽ������
                .timeToSend(timeToSend) // ��ʱ����ʱ��
                .build();
        return message;
    }
    private Message buildMessage2AndroidOther(String title, String content, JSONObject jsonObjectPayload, long timeToSend)
            throws Exception {

        Message message = new Message.Builder()
                .title(title)
                .description(content)
                .payload(jsonObjectPayload.toJSONString())
                .restrictedPackageName(ANDROID_PACKAGE_NAME)// ���ð���
                .passThrough(PASS_THROUGH) // ��Ϣʹ��͸����ʽ
                .notifyType(NOTIFY_TYPE) // ʹ��Ĭ����ʾ����ʾ
                .enableFlowControl(true) // ������Ϣ�Ƿ���Ҫ����ƽ������
                .timeToSend(timeToSend) // ��ʱ����ʱ��
                //.extra(Constants.EXTRA_PARAM_NOTIFY_FOREGROUND, "0")
                //.extra(Constants.EXTRA_PARAM_NOTIFY_EFFECT, Constants.NOTIFY_LAUNCHER_ACTIVITY)
                .build();

        return message;

    }

    /**
     * ����ios������Ϣ
     *
     * @param content
     * @param jsonObjectPayload
     * @param timeToSend
     * @return
     */
    private Message buildMessage2IOS(String content, JSONObject jsonObjectPayload, long timeToSend) throws Exception {
        Message message = new Message.IOSBuilder().description(content).badge(1) // ���ֽǱ�
                .extra("payload", jsonObjectPayload.toJSONString()).timeToSend(timeToSend).build();
        return message;
    }

    /**
     * ����������Ϣ
     *
     * @param title
     * @param content
     * @param jsonObjectPayload
     * @param deviceType
     * @param timeToSend
     * @return Message
     */
    private Message buildMessage(String title, String content, JSONObject jsonObjectPayload, int deviceType,
                                 long timeToSend) throws Exception {
        Message message = null;
        if (deviceType == TYPE_ANDROID) {
            message = buildMessage2Android(title, content, jsonObjectPayload, timeToSend);
        } else if (deviceType == TYPE_IOS) {
            message = buildMessage2IOS(content, jsonObjectPayload, timeToSend);
        }
        return message;
    }
    private Message buildMessageOther(String title, String content, JSONObject jsonObjectPayload, int deviceType,
                                      long timeToSend) throws Exception {
        Message message = null;
        if (deviceType == TYPE_ANDROID) {
            message = buildMessage2AndroidOther(title, content, jsonObjectPayload, timeToSend);
        } else if (deviceType == TYPE_IOS) {
            message = buildMessage2IOS(content, jsonObjectPayload, timeToSend);
        }
        return message;
    }

    /**
     * �������豸����������Ϣ
     *
     * @param title
     * @param content
     * @param jsonObjectPayload
     * @param deviceType
     * @param timeToSend
     * @throws Exception
     */
    public Result sendBroadcastAll(String title, String content, JSONObject jsonObjectPayload, int deviceType,
                                   long timeToSend) throws Exception {
        reStartPush(deviceType);// ׼��С������

        Sender sender = null;
        if (deviceType == TYPE_ANDROID) {
            sender = new Sender(ANDROID_APP_SECRET); // ��Ҫ����appSecert������
        } else if (deviceType == TYPE_IOS) {
            sender = new Sender(IOS_APP_SECRET); // ��Ҫ����appSecert������
        }
        Message message = buildMessage(title, content, jsonObjectPayload, deviceType, timeToSend);
        Result result = sender.broadcastAll(message, 0);// ������Ϣ�������豸��������

        return result;
    }

    /**
     * ����regid����һ������
     *
     * @param title
     * @param content
     * @param jsonObjectPayload
     * @param regId
     * @param deviceType
     * @throws Exception
     */
    public Result sendMessageToRegId(String title, String content, JSONObject jsonObjectPayload, String regId, int deviceType,
                                     long timeToSend) throws Exception {
        reStartPush(deviceType);// ׼��С������

        Sender sender = null;
        if (deviceType == TYPE_ANDROID) {
            sender = new Sender(ANDROID_APP_SECRET); // ��Ҫ����appSecert������
        } else if (deviceType == TYPE_IOS) {
            sender = new Sender(IOS_APP_SECRET); // ��Ҫ����appSecert������
        }
        Message message = buildMessage(title, content, jsonObjectPayload, deviceType, timeToSend);
        Result result = sender.send(message, regId, 0); // ����regID��������Ϣ��ָ���豸�ϣ������ԡ�
        return result;
    }
    public Result sendMessageToRegIdOther(String title, String content, JSONObject jsonObjectPayload, String regId, int deviceType,
                                          long timeToSend) throws Exception {
        reStartPush(deviceType);// ׼��С������

        Sender sender = null;
        if (deviceType == TYPE_ANDROID) {
            sender = new Sender(ANDROID_APP_SECRET); // ��Ҫ����appSecert������
        } else if (deviceType == TYPE_IOS) {
            sender = new Sender(IOS_APP_SECRET); // ��Ҫ����appSecert������
        }
        Message message = buildMessageOther(title, content, jsonObjectPayload, deviceType, timeToSend);
        Result result = sender.send(message, regId, 0); // ����regID��������Ϣ��ָ���豸�ϣ������ԡ�
        return result;
    }




    /**
     * ����alias����һ������
     *
     * @param title
     * @param content
     * @param jsonObjectPayload
     * @param userMobile
     * @param deviceType
     * @throws Exception
     */
    @SuppressWarnings("unused")
    private Result sendMessageToAlias(String title, String content, JSONObject jsonObjectPayload, String userMobile,
                                      int deviceType, long timeToSend) throws Exception{
        reStartPush(deviceType);// ׼��С������

        Sender sender = null;
        if (deviceType == TYPE_ANDROID) {
            sender = new Sender(ANDROID_APP_SECRET); // ��Ҫ����appSecert������
        } else if (deviceType == TYPE_IOS) {
            sender = new Sender(IOS_APP_SECRET); // ��Ҫ����appSecert������
        }
        Message message = buildMessage(title, content, jsonObjectPayload, deviceType, timeToSend);
        Result result = sender.sendToAlias(message, userMobile, 0); // ����alias��������Ϣ��ָ���豸�ϣ������ԡ�
        return result;
    }
}
